package com.zzh.custom.refresh;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class PullToRefreshLayout extends LinearLayout {


    private static final String TAG = PullToRefreshLayout.class.getSimpleName();

    private PullToRefreshConfig mPullToRefreshConfig;

    private RefreshStatus refreshStatus = RefreshStatus.END;

    // 手势判定限制值
    private int mScaledTouchSlop;

    // head的高度和宽度
    private int mHeadViewHeight, mHeadViewWidth;
    // foot的高度和宽度
    private int mFootViewHeight, mFootViewWidth;


    public PullToRefreshLayout(Context context) {
        this(context, null);
    }

    public PullToRefreshLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PullToRefreshLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        mScaledTouchSlop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        mPullToRefreshConfig = new PullToRefreshConfig();
        // 修改全部配置项
        updatePullToRefreshConfig(mPullToRefreshConfig);
    }

    private void updatePullToRefreshConfig(PullToRefreshConfig pullToRefreshConfig) {
        mPullToRefreshConfig = pullToRefreshConfig;
        // TODO: 2022/8/9 执行操作
        removeAllViews();
        // 添加底部view
        Foot foot = pullToRefreshConfig.getFoot();
        if (foot != null) {
            View footView = foot.createView(this);
            addView(footView, 0);
            foot.init();
            measureFootView(footView);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) footView.getLayoutParams();
        }
        // 添加内容去view
        Content content = pullToRefreshConfig.getContent();
        if (content != null) {
            addView(content.createView(this), 0);
            content.init();
        }
        // 添加头部view
        Head head = pullToRefreshConfig.getHead();
        if (head != null) {
            View headView = head.createView(this);
            addView(headView, 0);
            head.init();
            measureHeadView(headView);
            LinearLayout.LayoutParams layoutParams = (LayoutParams) headView.getLayoutParams();
            if (getOrientation() == VERTICAL) {
                layoutParams.topMargin = -mHeadViewHeight;
            } else if (getOrientation() == HORIZONTAL) {
                layoutParams.leftMargin = -mHeadViewWidth;
            }
        }
    }

    private void measureFootView(View footView) {
        final DisplayMetrics dm = getResources().getDisplayMetrics();
        footView.measure(
                View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.AT_MOST),
                View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.AT_MOST));
        mFootViewHeight = footView.getMeasuredHeight();
        mFootViewWidth = footView.getMeasuredWidth();
    }

    private void measureHeadView(View headView) {
        final DisplayMetrics dm = getResources().getDisplayMetrics();
        headView.measure(
                View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.AT_MOST),
                View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.AT_MOST));
        mHeadViewHeight = headView.getMeasuredHeight();
        mHeadViewWidth = headView.getMeasuredWidth();
    }

    public PullToRefreshConfig copyPullToRefreshConfig() {
        return mPullToRefreshConfig.copy();
    }

    private float mDownX;
    private float mDownY;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.i(TAG, "dispatchTouchEvent: " + ev.getY());
        View view = mPullToRefreshConfig.getContent().getView();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownX = ev.getX();
                mDownY = ev.getY();
                if (!view.isScrollContainer()) {
                    return true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (view.isScrollContainer()) {
                    if (getOrientation() == VERTICAL) {
                        // 垂直方向
                        if (view.canScrollVertically(-1)) {
                            // 到达下部
                        }
                        if (view.canScrollVertically(1)) {
                            // 到达上部
                        }
                    } else if (getOrientation() == HORIZONTAL) {
                        // 水平方向
                        if (view.canScrollHorizontally(-1)) {
                            // 到达左部
                        }
                        if (view.canScrollHorizontally(1)) {
                            // 到达右部
                        }
                    }
                } else {
                    // 不可滑动 直接执行
                    if (getOrientation() == VERTICAL) {
                        // 垂直方向
                        if (refreshStatus == RefreshStatus.END) {
                            // 执行按下
                            if (Math.abs(mDownY - ev.getY()) > mScaledTouchSlop) {
                                if (mDownY > ev.getY()) {
                                    refreshStatus = RefreshStatus.LOAD;
                                    Foot foot = mPullToRefreshConfig.getFoot();
                                    if (foot != null) {
                                        foot.onTouchDown(ev, mFootViewHeight, mFootViewWidth);
                                    }
                                } else {
                                    refreshStatus = RefreshStatus.REFRESH;
                                    Head head = mPullToRefreshConfig.getHead();
                                    if (head != null) {
                                        head.onTouchDown(ev, mHeadViewHeight, mHeadViewWidth);
                                    }
                                }
                            }
                        } else {
                            if (refreshStatus == RefreshStatus.REFRESH) {
                                Head head = mPullToRefreshConfig.getHead();
                                if (head != null) {
                                    head.onTouchMove(ev, mHeadViewHeight, mHeadViewWidth);
                                }
                            } else if (refreshStatus == RefreshStatus.LOAD) {
                                Foot foot = mPullToRefreshConfig.getFoot();
                                if (foot != null) {
                                    foot.onTouchMove(ev, mFootViewHeight, mFootViewWidth);
                                }
                            }
                        }

                    } else if (getOrientation() == HORIZONTAL) {
                        // 水平方向
                        if (refreshStatus == RefreshStatus.END) {
                            // 执行按下
                            if (Math.abs(mDownX - ev.getX()) > mScaledTouchSlop) {
                                if (mDownX > ev.getX()) {
                                    refreshStatus = RefreshStatus.LOAD;
                                    Foot foot = mPullToRefreshConfig.getFoot();
                                    if (foot != null) {
                                        foot.onTouchDown(ev, mFootViewHeight, mFootViewWidth);
                                    }
                                } else {
                                    refreshStatus = RefreshStatus.REFRESH;
                                    Head head = mPullToRefreshConfig.getHead();
                                    if (head != null) {
                                        head.onTouchDown(ev, mHeadViewHeight, mHeadViewWidth);
                                    }
                                }
                            }
                        } else {
                            if (refreshStatus == RefreshStatus.REFRESH) {
                                Head head = mPullToRefreshConfig.getHead();
                                if (head != null) {
                                    head.onTouchMove(ev, mHeadViewHeight, mHeadViewWidth);
                                }
                            } else if (refreshStatus == RefreshStatus.LOAD) {
                                Foot foot = mPullToRefreshConfig.getFoot();
                                if (foot != null) {
                                    foot.onTouchMove(ev, mFootViewHeight, mFootViewWidth);
                                }
                            }
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                refreshStatus = RefreshStatus.END;
                Foot foot = mPullToRefreshConfig.getFoot();
                if (foot != null) {
                    foot.onTouchUp(ev, mFootViewHeight, mFootViewWidth);
                }
                Head head = mPullToRefreshConfig.getHead();
                if (head != null) {
                    head.onTouchUp(ev, mHeadViewHeight, mHeadViewWidth);
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }


    public Head getHead() {
        return mPullToRefreshConfig.getHead();
    }

    public Foot getFoot() {
        return mPullToRefreshConfig.getFoot();
    }

    public Content getContent() {
        return mPullToRefreshConfig.getContent();
    }


}
